import Vue from "vue";
import VueRouter from "vue-router";
import store from "@/store";
Vue.use(VueRouter);

const routes = [
  { path: "/", redirect: "/index" },
  {
    path: "/index",
    component: () => import("@/views/index/index.vue"),
    redirect: "/home",
    children: [
      {
        path: "/home",
        component: () => import("@/views/home/index.vue"),
      },
      {
        path: "/partTime",
        component: () => import("@/views/partTime/index.vue"),
      },
      {
        path: "/communtiy",
        component: () => import("@/views/communtiy/index.vue"),
      },
      {
        path: "/personal",
        component: () => import("@/views/personal/index.vue"),
      },
      {
        path: "/backstage",
        component: () => import("@/views/backstage/index.vue"),
      },
    ],
  },
  {
    path: "/Adminlogin",
    component: () => import("@/views/admin/login/index.vue"),
  },
  {
    path: "/AdminReg",
    component: () => import("@/views/admin/reguser/index.vue"),
  },
  {
    path: "/layout",
    redirect: "layoutHome",
    component: () => import("@/views/admin/layout/index.vue"),
    children: [
      {
        path: "/layoutHome",
        component: () => import("@/views/admin/home/index.vue"),
      },
      {
        path: "/layoutStudent",
        component: () => import("@/views/admin/student/index.vue"),
      },
      {
        path: "/layoutMerchant",
        component: () => import("@/views/admin/merchant/index.vue"),
      },
      {
        path: "/layoutWantad",
        component: () => import("@/views/admin/wantad/index.vue"),
      },
      {
        path: "/layoutEvaluate",
        component: () => import("@/views/admin/evaluate/index.vue"),
      },
      {
        path: "/layoutCommunity",
        component: () => import("@/views/admin/community/index.vue"),
      },
    ],
  },
  {
    path: "/login",
    component: () => import("@/views/personal/login/index.vue"),
  },
  {
    path: "/reg",
    component: () => import("@/views/personal/reguser/index.vue"),
  },
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});

router.beforeEach((to, from, next) => {
  const token = store.state.token;
  const touritToken = store.state.touritToken;
  if (token && !store.state.userInfo.username) {
    // 有token但是没有用户信息, 才去请求用户信息保存到vuex里
    // 调用actions里方法请求数据
    store.dispatch("initUserInfo");
    // 下次切换页面vuex里有用户信息数据就不会重复请求用户信息
  }
  if (touritToken && !store.state.touristInfo.Touristusername) {
    store.dispatch("initGetTouristInfo");
  }
  next(); // 路由放行
});
export default router;
